//
//  DeviceViewModel.m
//  CityFire
//
//  Created by Liuyz on 2017/8/15.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "DeviceViewModel.h"
#import "UserInfoModel.h"
#import "NSUserDefaults+Tools.h"

@implementation DeviceViewModel

- (void)getAllDeviceInfo:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kDEVICES_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"userName" : model.userName, @"pageNo" : parameter[@"pageNo"], @"pageSize" : parameter[@"pageSize"]} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //userName=admin&pageNo=1&pageSize=10&sessionid=x
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            DevicesModel *devsModel = [MTLJSONAdapter modelOfClass:[DevicesModel class] fromJSONDictionary:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            self.devicesModel = devsModel;
            //
            //        if (userModel) {
            //            [NSUserDefaults writeUserDataWithKey:userModel.seeeionID forKey:kNSUSERDEFAULT_KEY_SESSIONID];
            //            [NSUserDefaults writeUserDataWithKey:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:kNSUSERDEFAULT_KEY_USERINFO];
            //        }
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                //            [SVProgressHUD showSuccessWithStatus:@"退出成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
}


- (void)getQueryPersonOrgs:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kQueryPersonOrgs_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"userName" : model.userName, @"pageNo" : @"1", @"pageSize" : @"200"} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //userName=admin&pageNo=1&pageSize=10&sessionid=x
            //        , @"pageNo" : parameter[@"pageNo"], @"pageSize" : parameter[@"pageSize"]
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            NSArray *orgsArray = [MTLJSONAdapter modelsOfClass:[DeviceModel class] fromJSONArray:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            self.personOrgsArray = orgsArray;
            //
            //        if (userModel) {
            //            [NSUserDefaults writeUserDataWithKey:userModel.seeeionID forKey:kNSUSERDEFAULT_KEY_SESSIONID];
            //            [NSUserDefaults writeUserDataWithKey:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:kNSUSERDEFAULT_KEY_USERINFO];
            //        }
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                //            [SVProgressHUD showSuccessWithStatus:@"退出成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
}
//
- (void)getQueryPersonOrgDevices:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kQueryPersonOrgDevices_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"userName" : model.userName, @"pageNo" : @"1", @"pageSize" : @"200", @"orgname" : parameter[@"orgname"]} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //userName=admin&pageNo=1&pageSize=10&sessionid=x
            //        , @"pageNo" : parameter[@"pageNo"], @"pageSize" : parameter[@"pageSize"]
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            NSArray *orgsArray = [MTLJSONAdapter modelsOfClass:[DeviceModel class] fromJSONArray:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            self.devicesArray = orgsArray;
            //
            //        if (userModel) {
            //            [NSUserDefaults writeUserDataWithKey:userModel.seeeionID forKey:kNSUSERDEFAULT_KEY_SESSIONID];
            //            [NSUserDefaults writeUserDataWithKey:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:kNSUSERDEFAULT_KEY_USERINFO];
            //        }
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                //            [SVProgressHUD showSuccessWithStatus:@"退出成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
}


- (void)getPersonDeviceCount:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kPersonGetDeviceInfo_DEAL_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"username" : model.userName} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            DevicesModel *devicesModel = [MTLJSONAdapter modelOfClass:[DevicesModel class] fromJSONDictionary:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            self.devicesModel = devicesModel;
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showSuccessWithStatus:error.domain];
        }];
    }
    
}

@end
